﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;

namespace TCGWeb
{
    public class Game
    {
        public string Name { get; set; }

        public int CountPlayers
        {
            get
            {
                return PlayersIp.Count;
            }
        }
        public List<string> PlayersIp = new List<string>();

        public object ThreadId;
    }

    public static class ApplicationConstants
    {
        public const string Games = "Games";
    }

    public partial class StartPage : System.Web.UI.Page
    {

        private List<Game> games = new List<Game>();

        protected void Page_Load(object sender, EventArgs e)
        {
            if (Application[ApplicationConstants.Games] != null)
                games = Application[ApplicationConstants.Games] as List<Game>;
            if (!IsPostBack && games != null && games.Count > 0)
                BindData();
        }

        private void BindData()
        {
            rptAvailableGames.DataSource = games;
            rptAvailableGames.DataBind();
        }

        protected void btnCreateGame_Click(object sender, EventArgs e)
        {
            if (!String.IsNullOrEmpty(tbxCreateGame.Text.Trim()) && games.Where(g => g.Name == tbxCreateGame.Text).Count() == 0)
            {
                var newGame = new Game() { Name = tbxCreateGame.Text };
                newGame.PlayersIp.Add(Request.ServerVariables["HTTP_X_FORWARDED_FOR"] ?? Request.ServerVariables["REMOTE_ADDR"]);
                games.Add(newGame);

                Application[ApplicationConstants.Games] = games;
                BindData();
            }
        }

        protected void lbtJoinGame_Command(object sender, CommandEventArgs e)
        {
            var gameName = e.CommandArgument as string;

        }

    }
}